package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Survey;
import com.itheima.service.SurveyService;
import com.itheima.service.impl.SurveyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/survey/*")
public class SurveyServlet extends BaseServlet{
    private SurveyService surveyService = new SurveyServiceImpl();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 调用service查询
        List<Survey> surveys = surveyService.selectAll();

        //2. 转为JSON
        String jsonString = JSON.toJSONString(surveys);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1. 接收品牌数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串


        //转为survey对象
        Survey survey = JSON.parseObject(params, Survey.class);

        //2. 调用service添加
        surveyService.add(survey);

        //3. 响应成功的标识
        response.getWriter().write("success");
    }

    /**
     * 批量删除
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1. 接收数据  [1,2,3]
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串

        //转为 int[]
        int[] ids = JSON.parseObject(params, int[].class);

        //2. 调用service添加
        surveyService.deleteByIds(ids);

        //3. 响应成功的标识
        response.getWriter().write("success");
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串

        System.out.println(params);

        /*
        char[] idstr = params.toCharArray();
        ArrayList<String> idstr2 = new ArrayList<String>();
        for (int i = 0;i<idstr.length;i++){
            for (int j = 0;j<=9;j++){
                if (i==j){
                    idstr2.add(String.valueOf(idstr[i]));
                }
            }
        }


        String id = idstr2.toString();

         */


        //2.调用service删除

        //System.out.println(id);

        //surveyService.delete(id);
        //System.out.println(id);
        surveyService.delete(params);






        //3. 响应成功的标识
        response.getWriter().write("success");
    }

    /**
     * 分页查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        String _creatorId = request.getParameter("creatorId");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        PageBean<Survey> pageBean = new PageBean<>();

        if(_creatorId==null){
            //2. 调用service查询
            pageBean = surveyService.MselectByPage(currentPage, pageSize);
        }else {
            int creatorId = Integer.parseInt(_creatorId);
            //2. 调用service查询
            pageBean = surveyService.selectByPage(currentPage, pageSize,creatorId);
        }


        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }








    /**
     * 分页条件查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        // 获取查询条件对象
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串

        //转为 Brand
        Survey survey = JSON.parseObject(params, Survey.class);


        //2. 调用service查询
        PageBean<Survey> pageBean = surveyService.selectByPageAndCondition(currentPage,pageSize,survey);

        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void selectBystatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);
        PageBean<Survey> pageBean = surveyService.selectBystatus(currentPage, pageSize);

        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        System.out.println(pageBean);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);

    }



}
